/*******************************************************************
 *
 * Copyright (C), 2021-2022, LS, All rights reserved.
 *
 * Author      : 
 * Create Time : 2021-09-22
 * Version     : v0.1
 * Description : 
 *******************************************************************/
 
/*----------------------------Head file----------------------------*/
#include "hal_hall.h"
#include "bsp_hw_config.h"


#define HAL_HALL_LOG_EN     0

#if HAL_HALL_LOG_EN
#include "log.h"
#define HAL_HALL_LOG        LOG
#else
#define HAL_HALL_LOG(...)
#endif

/*----------------------------macro file---------------------------*/

/*----------------------------type define--------------------------*/

/*----------------------------var define---------------------------*/
static hal_hall_evt_cb_t hall_evt_cb = NULL;

/*-------------------------func declaration------------------------*/

/*-----------------------------------------------------------------*/

static void hall_pin_int_hdl(GPIO_Pin_e pin, IO_Wakeup_Pol_e type)
{
    if(hall_evt_cb) {
        if(NEGEDGE == type) {
            if(BSP_HALL1_PIN_NUM == pin) {
                hall_evt_cb(1, true);
            }
            else if(BSP_HALL2_PIN_NUM == pin) {
                hall_evt_cb(2, true);
            }
#if(HALL3_PIN_ENABLE)
            else if(BSP_HALL3_PIN_NUM == pin) {
                hall_evt_cb(3, true);
            }
#endif
        }
        else {
            if(BSP_HALL1_PIN_NUM == pin) {
                hall_evt_cb(1, false);
            }
            else if(BSP_HALL2_PIN_NUM == pin) {
                hall_evt_cb(2, false);
            }
#if(HALL3_PIN_ENABLE)
            else if(BSP_HALL3_PIN_NUM == pin) {
                hall_evt_cb(3, false);
            }
#endif
        }
    }
}


int hal_hall_hw_init(hal_hall_evt_cb_t cb)
{
    int ret = PPlus_SUCCESS;

    ret += hal_gpio_pin_init(BSP_HALL1_PIN_NUM, GPIO_INPUT);
    hal_gpio_pull_set(BSP_HALL1_PIN_NUM, GPIO_PULL_UP);
    ret += hal_gpioin_register(BSP_HALL1_PIN_NUM, hall_pin_int_hdl, hall_pin_int_hdl);
    
    ret += hal_gpio_pin_init(BSP_HALL2_PIN_NUM, GPIO_INPUT);
    hal_gpio_pull_set(BSP_HALL2_PIN_NUM, GPIO_PULL_UP);
    ret += hal_gpioin_register(BSP_HALL2_PIN_NUM, hall_pin_int_hdl, hall_pin_int_hdl);
    
#if(HALL3_PIN_ENABLE)
    ret += hal_gpio_pin_init(BSP_HALL3_PIN_NUM, GPIO_INPUT);
    hal_gpio_pull_set(BSP_HALL3_PIN_NUM, GPIO_PULL_UP);
    ret += hal_gpioin_register(BSP_HALL3_PIN_NUM, hall_pin_int_hdl, hall_pin_int_hdl);
#endif

    hall_evt_cb = cb;

    return ret;
}


int hal_hall_hw_deinit(void)
{
    int ret = PPlus_SUCCESS;

    ret += hal_gpio_pin_init(BSP_HALL1_PIN_NUM, GPIO_INPUT);
    hal_gpio_pull_set(BSP_HALL1_PIN_NUM, FLOATING);
    ret += hal_gpioin_unregister(BSP_HALL1_PIN_NUM);
    
    ret += hal_gpio_pin_init(BSP_HALL2_PIN_NUM, GPIO_INPUT);
    hal_gpio_pull_set(BSP_HALL2_PIN_NUM, FLOATING);
    ret += hal_gpioin_unregister(BSP_HALL2_PIN_NUM);
    
#if(HALL3_PIN_ENABLE)
    ret += hal_gpio_pin_init(BSP_HALL3_PIN_NUM, GPIO_INPUT);
    hal_gpio_pull_set(BSP_HALL3_PIN_NUM, FLOATING);
    ret += hal_gpioin_unregister(BSP_HALL3_PIN_NUM);
#endif

    hall_evt_cb = NULL;

    return ret;
}

